﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestXNA3.Resources.Meshes;
using Microsoft.Xna.Framework;
using TestXNA3.Resources.Shaders;
using Microsoft.Xna.Framework.Graphics;
using XMas;
using System.Runtime.Serialization;

namespace TestXNA3.Components
{
    [Serializable]
    public class SkeletalMeshComponent : MeshComponent
    {
        private SkeletalMesh _skeletalMesh = null;
        public SkeletalMesh SkeletalMesh
        {
            get { return _skeletalMesh; }
        }

        public XMAnimationSystem AnimTree
        {
            get { return _skeletalMesh.AnimTree; }
        }

        private Matrix GetRecursiveBoneTransform(ModelBone bone)
        {
            Matrix m = bone.Transform;
            //if (bone.Parent != null)
            //{
                //m *= GetRecursiveBoneTransform(bone.Parent);
            //}
            return m;
        }

        public SkeletalMeshComponent(string meshName, string animTreeName)
        {
            _skeletalMesh = new SkeletalMesh(meshName, animTreeName);
            UseAsParentTracer = false;
        }

        public override void Update(float dt)
        {
            base.Update(dt);
            _skeletalMesh.AnimTree.Update(GameStartup.GameTime.ElapsedGameTime);
        }

        public override void AddToRenderQueue(TestXNA3.Render.SceneRenderer sceneRenderer, ref List<TestXNA3.Resources.MeshResource> meshes)
        {
            if (_skeletalMesh == null)
                return;

            _skeletalMesh.BoundingSphere = new BoundingSphere(Parent.Location, 999.0f);//Microsoft.Xna.Framework.BoundingBox(-Vector3.One * 999.0f, Vector3.One * 999f);
            _skeletalMesh.CastShadows = true;
            _skeletalMesh.World = Matrix.CreateTranslation(Translation) * Parent.Axis;

            meshes.Add(_skeletalMesh);
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
        }
    }
}
